"""HCP database models."""

# XXX: Test binary value.

import ldap
from django.db import models

class Modification(models.Model):

    username = models.CharField(max_length=40)
    timestamp = models.DateTimeField(auto_now_add=True)

MOD_ACTIONS = (
    ('A', 'added'),
    ('D', 'removed'),
    ('M', 'modified'),
    ('R', 'renamed'),
)

class ModificationObject(models.Model):

    mod = models.ForeignKey(Modification)
    action = models.CharField(max_length=1, choices=MOD_ACTIONS)
    object_type = models.CharField(max_length=40)
    dn = models.CharField(max_length=2048)

MOD_OPS = (
    ('A', 'add'),
    ('D', 'delete'),
    ('M', 'replace'),
)

MOD_OP_MAP = {
    ldap.MOD_ADD: 'A',
    ldap.MOD_DELETE: 'D',
    ldap.MOD_REPLACE: 'R',
}

class ModificationAttribute(models.Model):

    mod_obj = models.ForeignKey(ModificationObject)
    attribute_name = models.CharField(max_length=64)
    op = models.CharField(max_length=1, choices=MOD_OPS)

class ModificationValueOld(models.Model):

    mod_attr = models.ForeignKey(ModificationAttribute)
    value = models.TextField()

class ModificationValueNew(models.Model):

    mod_attr = models.ForeignKey(ModificationAttribute)
    value = models.TextField()


#    Add Modlist
#    ===========
#    The add modlist is a list of ``(attribute_name, value)`` tuples.  The value
#    will be a string for single-value attributes, or a list.  This is the same
#    as the Python LDAP API.
#
#    Modify Modlist
#    ==============
#    The modify modlist is slightly different from the Python LDAP API.  It also
#    includes the old value for your convenience.  It is a list of ``(mod_op,
#    attribute_name, old_value, new_value)`` tuples.  The ``mod_op`` is one of
#    ``ldap.MOD_ADD``, ``ldap.MOD_DELETE``, or ``ldap.MOD_REPLACE``.
#    ``old_value`` will be None if it was not previously set, and conversely
#    ``new_value`` will be None if the attribute is being deleted.
